package eventloop;

import org.apache.log4j.Logger;
import rtmp.RtmpResponse;
import user.Receive;
import user.ReceiveGroup;
import util.Common;
import util.MsgType;

import java.util.List;

public class AudioRunnable implements Runnable{

    private byte[] keyData;

    private final static Logger logger = Logger.getLogger(AudioRunnable.class);

    private byte[] message;
    private int timestamp;
    private List<Receive> list;

    public AudioRunnable(byte[] message, List<Receive> list, int timestamp) {
        this.message = message;
        this.list = list;
        this.timestamp = timestamp;
    }


    @Override
    public void run() {
        try {
//             audioStreamDecoder.decode(message);
            if (message[1] == 0x00){
                keyData = message;
            }
            if (list != null) {
                for (Receive receive : list) {
                    if (receive.ready && receive.keyframe) {
                        if (!receive.playing) {
                            continue;
                        }
                        if (!receive.audioKeyFrame){
                            logger.info("进来");
//                        int sendTime = timestamp - receive.extTimestampNum * Common.TIMESTAMP_MAX_NUM;
//                        if(sendTime > Common.TIMESTAMP_MAX_NUM){
//                            receive.extTimestampNum = timestamp / Common.TIMESTAMP_MAX_NUM;
//                            sendTime = timestamp;
//                        }
//                        RtmpResponse.sendData(keyData, MsgType.MSG_AUDIO, 1337, receive.receive, sendTime,receive.check_size);

                            if(timestamp < Common.TIMESTAMP_MAX_NUM){
                                RtmpResponse.sendData(keyData, MsgType.MSG_AUDIO, 8, receive.receive, timestamp,receive.check_size);
                            } else {
                                int sendTime = timestamp - receive.prevTimestamp;
                                if(sendTime >= Common.TIMESTAMP_MAX_NUM){
                                    RtmpResponse.sendData(keyData, MsgType.MSG_AUDIO, 8, receive.receive, timestamp,receive.check_size);
                                } else {
                                    RtmpResponse.sendDataExtTimestamp(keyData, MsgType.MSG_AUDIO, 8, receive.receive, sendTime,receive.check_size);
                                }
                            }
                            receive.audioKeyFrame = true;
                        }
//                    int sendTime = timestamp - receive.extTimestampNum * Common.TIMESTAMP_MAX_NUM;
//                    if(sendTime > Common.TIMESTAMP_MAX_NUM){
//                        receive.extTimestampNum = timestamp / Common.TIMESTAMP_MAX_NUM;
//                        sendTime = timestamp;
//                    }
//                    RtmpResponse.sendData(message, MsgType.MSG_AUDIO, 1337, receive.receive, sendTime,receive.check_size);

                        if(timestamp < Common.TIMESTAMP_MAX_NUM){
                            RtmpResponse.sendData(message, MsgType.MSG_AUDIO, 8, receive.receive, timestamp,receive.check_size);
                        } else {
                            int sendTime = timestamp - receive.prevTimestamp;
                            if(sendTime >= Common.TIMESTAMP_MAX_NUM){
                                RtmpResponse.sendData(message, MsgType.MSG_AUDIO, 8, receive.receive, timestamp,receive.check_size);
                            } else {
                                RtmpResponse.sendDataExtTimestamp(message, MsgType.MSG_AUDIO, 8, receive.receive, sendTime,receive.check_size);
                            }
                        }

                        if(timestamp >= Common.TIMESTAMP_MAX_NUM){
                            receive.prevTimestamp = timestamp;
                        }
                    }
                }
            }
        }catch (Exception e) {
            logger.info(e.getMessage());
        }
    }
}
